package com.bjpowernode.controller.advice;
import com.bjpowernode.exception.MyDateException;
import com.bjpowernode.exception.MyLengthException;
import com.bjpowernode.exception.MyLoginException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.HashMap;
//增强控制器
//全局异常控制器（处理异常）
//@ControllerAdvice
@RestControllerAdvice
public class MyControllerAdvice {
    @ExceptionHandler(MyLengthException.class)
    public HashMap<String, Object> myException(MyLengthException myLengthException){
        HashMap<String, Object> map = new HashMap<>();
        map.put("result", "error");
        map.put("mess", myLengthException.getMessage());
        return map;
    }

    @ExceptionHandler(MyDateException.class)
    public HashMap<String, Object> myLoginException(MyDateException myDateException){
        HashMap<String, Object> map = new HashMap<>();
        map.put("result", "error");
        map.put("mess", myDateException.getMessage());
        return map;
    }

    @ExceptionHandler(SQLException.class)
    public HashMap<String, Object> mySqlException(SQLException sqlException){
        sqlException.printStackTrace();
        HashMap<String, Object> map = new HashMap<>();
        map.put("result", "error");
        map.put("mess", sqlException.getMessage());
        return map;
    }

    @ExceptionHandler(MyLoginException.class)
    public HashMap<String, Object> myLoginException(MyLoginException myLoginException){
        HashMap<String, Object> map = new HashMap<>();
        map.put("result", "error");
        map.put("mess", myLoginException.getMessage());
        return map;
    }




}
